//
//  BaseNavViewController.m
//  Fruit
//
//  Created by ༺ོ࿆飞༻ on 2020/2/2.
//  Copyright © 2020 ༺ོ࿆飞༻. All rights reserved.
//

#import "BaseNavViewController.h"

@interface BaseNavViewController ()<UINavigationControllerDelegate,UIGestureRecognizerDelegate>


@end

@implementation BaseNavViewController

/// 当前的导航控制器是否可以旋转
-(BOOL)shouldAutorotate{
 
    return YES;
}
 
//设置支持的屏幕旋转方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
 
    return self.interfaceOrientationMask;
}
 
//设置presentation方式展示的屏幕方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
 
    return self.interfaceOrientation;
}


- (UIStatusBarStyle)preferredStatusBarStyle {
    UIViewController *topVC = self.topViewController;
    return [topVC preferredStatusBarStyle];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *imgView =  [self findHairlineImageViewUnder:self.navigationBar];
    imgView.hidden = YES;
    
    __weak __typeof(self) weakSelf = self;
   if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
   {
       // UIScreenEdgePanGestureRecognizer
       self.interactivePopGestureRecognizer.delegate = weakSelf;
       self.delegate = weakSelf;
   }
}
-(id)initWithRootViewController:(UIViewController *)rootViewController{
    
    if (self = [super initWithRootViewController:rootViewController]) {
        self.navigationBar.translucent = NO;
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : RGB34, NSFontAttributeName : FONT(17)}];
        [self.navigationController.navigationBar setBackIndicatorImage:[UIImage imageNamed:@"nav_back_icon"]];
        [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"nav_back_icon"]];
        self.navigationBar.barTintColor = kWhiteColor;
        self.navigationBar.tintColor = RGB34;
    }
    return self;
}


//通过一个方法来找到这个黑线(findHairlineImageViewUnder):
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.childViewControllers.count != 0) {
           viewController.hidesBottomBarWhenPushed = YES;
       }
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]){
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    [super pushViewController:viewController animated:animated];
}
#pragma mark UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animate
{
    // Enable the gesture again once the new controller is shown
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
        self.interactivePopGestureRecognizer.enabled = YES;
}


#pragma mark UINavigationControllerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if ([self.childViewControllers count] == 1) {
        return NO;
    }
    return YES;
}

// ViewController接受多个手势，再处理手势冲突问题。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

// 处理手指在滑动的时候，被pop的 ViewController 中的 UIScrollView 会跟着一起滚动问题
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
}

@end
